package com.gzhu.knowledgeAdmin.controller;

// com/controller/TableController.java


import com.gzhu.knowledgeAdmin.common.result.R;
import com.gzhu.knowledgeAdmin.entity.Column;
import com.gzhu.knowledgeAdmin.entity.TableLabel;
import com.gzhu.knowledgeAdmin.service.impl.TableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/tables")
public class TableController {

    @Autowired
    private TableService tableService;

    @GetMapping
    public R getAllTableNames() {
        return R.ok(tableService.getAllTableNames());
    }


    @GetMapping("/{tableName}/columns")
    public R getTableColumns(@PathVariable String tableName) {
        List<Column> columns = tableService.getTableColumns(tableName);
        System.out.println(columns); // 打印列信息以进行调试
        return R.ok(columns);
    }

    @GetMapping("/{tableName}")
    public R getTableData(@PathVariable String tableName) {
        // 调用服务层方法获取表数据
        return R.ok(tableService.getTableData(tableName));
    }

    @ExceptionHandler(IllegalArgumentException.class)
    public R handleIllegalArgumentException(IllegalArgumentException e) {
        return R.ok(ResponseEntity.badRequest().body(e.getMessage()));
    }
    @GetMapping("/{tableName}/search")
    public R searchTableData(
            @PathVariable String tableName,
            @RequestParam String searchTerm,
            @RequestParam List<String> columns) {
        if(columns == null || columns.isEmpty()) {
//            return ResponseEntity.badRequest().body("No columns selected for search");
            return R.error();
        }
        return R.ok(tableService.searchTableData(tableName, columns, searchTerm));
    }

    @PostMapping("/{tableName}/insert")
    public R insertDataIntoTable(@PathVariable String tableName, @RequestBody Map<String, Object> data) {
        try {
            // 请确保您在TableService中已经实现了insertDataIntoTable方法。
            tableService.insertDataIntoTable(tableName, data);
            return R.ok();
        } catch (Exception e) {
            return R.error();
        }
    }

    @PostMapping("/{tableName}/delete")
    public R deleteRows(@PathVariable String tableName, @RequestBody List<Integer> idsToDelete) {
        try {
            tableService.deleteRowsByIds(tableName, idsToDelete);
            return R.ok();
        } catch (Exception e) {
            return R.error();
        }
    }

    @PutMapping("/{tableName}/{fId}")
    public R updateRecord(@PathVariable String tableName, @PathVariable Integer fId, @RequestBody Map<String, Object> data) {
        try {
            int updatedRows = tableService.updateRecord(tableName, fId, data);
            if (updatedRows > 0) {
                return R.ok();
            } else {
                return R.error();
            }
        } catch (Exception e) {
            return R.error();
        }
    }

    @GetMapping("/table-labels")
    public R getTableLabels() {
        try {
            List<TableLabel> labels = tableService.findAllTableLabels();
            return R.ok(labels);
        } catch (Exception e) {
            return R.error();
        }
    }
    // 其他增删改查和搜索的方法
}
